package com.gexiaobao.pigeon.ui.breeding_pigeon

import android.os.Build
import android.os.Bundle
import android.widget.HorizontalScrollView
import androidx.annotation.RequiresApi
import androidx.core.view.isVisible
import androidx.viewpager2.widget.ViewPager2
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseActivity
import com.gexiaobao.pigeon.app.model.bean.BloodBookDetailResponse
import com.gexiaobao.pigeon.app.util.StatusBarUtil
import com.gexiaobao.pigeon.databinding.ActivityPedigreeStyleBinding
import com.gexiaobao.pigeon.ui.adapter.BannerAdapter
import com.gexiaobao.pigeon.viewmodel.BreedingPigeonViewModel
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat
import me.hgj.jetpackmvvm.util.LogUtils
import me.hgj.jetpackmvvm.util.startActivity


/**
 * @Author      : hxw
 * @Date        : 2024/9/20 10:38
 * @Describe    : 血统书样式
 */
class ActivityPedigreeStyle : BaseActivity<BreedingPigeonViewModel, ActivityPedigreeStyleBinding>() {

    private var typeGeneration = ""
    private lateinit var mInfoDetail: BloodBookDetailResponse
    private var mId = 0

    private val mImageList = arrayListOf(
        R.mipmap.icon_three_generations_bg,
        R.mipmap.icon_four_generations_bg,
        R.mipmap.icon_four_vertical_generations_bg,
        R.mipmap.icon_five_generations_bg
    )


    @RequiresApi(Build.VERSION_CODES.M)
    override fun initView(savedInstanceState: Bundle?) {
        StatusBarUtil.setTranslucentForImageView(this, 0, null)
        mDatabind.includeBar.tvTitle.text = getString(R.string.choose_style)
        mInfoDetail = intent.getParcelableExtra("dataBean")!!
        mId = intent.getIntExtra("id", 0)
        initBannerData()
    }

    private val pageChangeCallback = object : ViewPager2.OnPageChangeCallback() {
        override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
            super.onPageScrolled(position, positionOffset, positionOffsetPixels)
            // 这里处理页面滑动的逻辑
        }

        override fun onPageSelected(position: Int) {
            super.onPageSelected(position)
            when (position) {
                0 -> refreshDataUi(3)
                1 -> refreshDataUi(4)
                2 -> refreshDataUi(41)
                else -> refreshDataUi(5)
            }
            LogUtils.debugInfo(position.toString())
        }

        override fun onPageScrollStateChanged(state: Int) {
            super.onPageScrollStateChanged(state)
            // 这里处理页面滑动状态改变的逻辑
        }
    }

    private fun initBannerData() {
        val adapter = BannerAdapter(mImageList)
        mDatabind.viewPager.adapter = adapter
        // 设置页面转换效果
//        mDatabind.viewPager.setPageTransformer(GalleryPageTransformer())

        mDatabind.viewPager.registerOnPageChangeCallback(pageChangeCallback)
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(
            mDatabind.includeBar.ivBack,
            mDatabind.btnChooseBloodBook,
            mDatabind.conThreeGeneration,
            mDatabind.conFourGeneration,
            mDatabind.conFourVerticalGeneration,
            mDatabind.conFiveGeneration
        ) {
            when (it) {
                mDatabind.conFiveGeneration -> refreshDataUi(5)
                mDatabind.conFourVerticalGeneration -> refreshDataUi(41)
                mDatabind.conFourGeneration -> refreshDataUi(4)
                mDatabind.conThreeGeneration -> refreshDataUi(3)
                mDatabind.btnChooseBloodBook -> startActivity<ActivityPedigreeStyleInfo>(
                    "dataBean" to mInfoDetail,
                    "typeGeneration" to typeGeneration,
                    "id" to mId
                )

                mDatabind.includeBar.ivBack -> finish()
            }
        }
    }

    private fun refreshDataUi(type: Int) {
        val bannerPosition: Int
        val isVisibleThree: Boolean
        val isVisibleFour: Boolean
        val isVisibleFourVertical: Boolean
        val isVisibleFive: Boolean
        when (type) {
            3 -> {
                isVisibleThree = true
                isVisibleFour = false
                isVisibleFourVertical = false
                isVisibleFive = false
                bannerPosition = 0
                typeGeneration = "three"
                mDatabind.horizontalScrollView.scrollTo(0, 0)
            }

            4 -> {
                isVisibleFour = true
                isVisibleThree = false
                isVisibleFourVertical = false
                isVisibleFive = false
                bannerPosition = 1
                typeGeneration = "four"
            }

            41 -> {
                isVisibleFourVertical = true
                isVisibleFour = false
                isVisibleThree = false
                isVisibleFive = false
                bannerPosition = 2
                typeGeneration = "fourV"
            }

            else -> {
                isVisibleFive = true
                isVisibleFourVertical = false
                isVisibleFour = false
                isVisibleThree = false
                bannerPosition = 3
                typeGeneration = "five"
                mDatabind.horizontalScrollView.fullScroll(HorizontalScrollView.FOCUS_RIGHT)
            }
        }
        mDatabind.viewPager.currentItem = bannerPosition
        mDatabind.ivSelectedBook.isVisible = isVisibleThree
        mDatabind.ivSelectedBookFour.isVisible = isVisibleFour
        mDatabind.ivSelectedBookVerticalFour.isVisible = isVisibleFourVertical
        mDatabind.ivSelectedBookFive.isVisible = isVisibleFive
    }
}